<?php

/**
 * Bic_Db_Driver
 * @package Bic
 * @author Bic
 * @version $Id:  2010-12-29
 * @copyright (c) 2010 Bic
 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
 *
 * Minimum Requirement: PHP 5.2.+
 */
if (!defined('IN_BIC')) {
    die('非法数据库驱动访问来源!');
}

class Bic_Db_Driver extends Bic_Base {

    /**
     * Array of MySQL engine instance .
     * @var type 
     */
    private static $_sInstance = array();
    
    /**
     *驱动信息
     * 包含驱动名
     * @var type 
     */
    private static $_sDriverInfo = array();

    /**
     * 建立数据库连接工厂方法
     * @param type $driver  数据库驱动
     * @param type $key     数据库key：如果想建立多个数据库连接，就指定不一样的key
     * @return \Bic_Db_Drivers_Mysqli 
     */
    public static function factory($driver, $key = '') {
        $keyMd5 = self::_createKey($key);
        if (isset(self :: $_sInstance[$keyMd5])) {
            return self :: $_sInstance[$keyMd5];
        }
        switch ($driver) {
            case 'mysql':
                break;
            case 'mysqli':
                self :: $_sInstance[$keyMd5] = Bic_Db_Drivers_Mysqli::getInstance($key);
                //给驱动添加当前key
                //用于关联相关信息，如：当前使用的驱动（mysql,mysqli...）
                self :: $_sInstance[$keyMd5]->set('_instanceKey',$keyMd5);
                self :: setDriverInfo($keyMd5,array('driver'=>$driver));
                return self :: $_sInstance[$keyMd5];
                break;
        }
        return false;
    }
    
    public static function _createKey($keyName){
        static $done='';
        if(!empty($done[$keyName])){
            return $done[$keyName];
        }else{
            if (empty($keyName)){
                $keyName = 'default';
            }
            return md5($keyName);
        }
    }
    
    public static function setDriverInfo($keyMd5,$info=array()){
        self :: $_sDriverInfo[$keyMd5] = $info;
    }

    public static function getDriverInfo($keyMd5,$infoKey='') {
        if (isset(self :: $_sDriverInfo[$keyMd5][$infoKey]))
            return self :: $_sDriverInfo[$keyMd5][$infoKey];
        return ;
    }

}

?>